<template>
  <div class="search">
    <div class="container">
      <div class="corner">
        <el-breadcrumb separator="/">
          <el-breadcrumb-item :to="{ path: '/' }">首页</el-breadcrumb-item>
          <el-breadcrumb-item>{{ title }}</el-breadcrumb-item>
        </el-breadcrumb>
      </div>
      <div class="tab">
        <ul class="tab-list">
          <li :class="{ pitch: active == 0 }" @click="active = 0">热点资讯</li>
          <li :class="{ pitch: active == 1 }" @click="active = 1">国家政策</li>
          <li :class="{ pitch: active == 2 }" @click="active = 2">业内活动</li>
        </ul>
      </div>
      <div class="list-box">
        <ul>
          <li
            v-for="item in listNewsData"
            :key="item.id"
            class="detail-list"
            @click="GoDetail(item.id)"
          >
            <div class="box">
              <div class="left-contiainer">
                <img
                  v-if="item.coverImage"
                  :src="item.coverImage"
                  alt=""
                  class="avatar"
                >
                <div v-else class="no-img" />
              </div>

              <div class="right">
                <h5 class="title">
                  {{ item.newsTitle }}
                </h5>
                <div class="word-break text">
                  {{ item.newsIntroduct }}
                </div>
                <div class="time">
                  {{ parseTime(item.createTime, "{y}-{m}-{d}") }}
                </div>
              </div>
            </div>
          </li>
        </ul>
      </div>
      <div v-show="active == 0" class="list-box">
        <div class="page">
          <!-- 分页 -->
          <el-pagination
            background
            layout="prev, pager, next"
            :page-sizes="[2, 5, 10, 20]"
            :page-size="this.consultPage.pageSize"
            :total="this.consultPage.total"
            @size-change="handleSizeChange"
            @current-change="handleCurrentChange"
          />
        </div>
      </div>
      <div v-show="active == 1" class="list-box">
        <div class="page">
          <!-- 分页 -->
          <el-pagination
            background
            layout="prev, pager, next"
            :page-sizes="[2, 5, 10, 20]"
            :page-size="this.policyPage.pageSize"
            :total="this.policyPage.total"
            @size-change="handleSizeChange"
            @current-change="handleCurrentChange"
          />
        </div>
      </div>
      <div v-show="active == 2" class="list-box">
        <div class="page">
          <!-- 分页 -->
          <el-pagination
            background
            layout="prev, pager, next"
            :page-sizes="[2, 5, 10, 20]"
            :page-size="this.IndustryPage.pageSize"
            :total="this.IndustryPage.total"
            @size-change="handleSizeChange"
            @current-change="handleCurrentChange"
          />
        </div>
      </div>
    </div>
  </div>
</template>

<script>
import { listNews } from '@/api/hot-consulting'
export default {
  data() {
    return {
      active: 0,
      title: '',
      listNewsData: [],
      // 咨询分页
      consultPage: {
        pageSize: 4,
        pageIndex: 1,
        total: 0
      },
      // 国家政策
      policyPage: {
        pageSize: 6,
        pageIndex: 1,
        total: 0
      },
      // 业内活动
      IndustryPage: {
        pageSize: 8,
        pageIndex: 1,
        total: 0
      }
    }
  },
  watch: {
    active(newActive) {
      this.judge()
    }
  },
  created() {
    this.active = this.$route.query.type
    this.judge()
  },
  methods: {
    judge() {
      if (this.active == 0) {
        this.title = '热点资讯'
        this.listNews(3, 'listNewsData')
      }
      if (this.active == 1) {
        this.title = '国家政策'
        this.policyNews(2, 'policyNewsData')
      }
      if (this.active == 2) {
        this.title = '业内活动'
        this.industryNews(1, 'industryNewsData')
      }
    },
    // 获取咨询列表
    listNews(type, name) {
      listNews(
        this.consultPage.pageIndex,
        this.consultPage.pageSize,
        type
      ).then(res => {
        this.consultPage.total = res.data.total
        this.listNewsData = res.data.records
        console.log(res)
        const d = JSON.stringify(res.data.records)
        localStorage.setItem(name, d)
      })
    },
    // 获取政策列表
    policyNews(type, name) {
      listNews(this.policyPage.pageIndex, this.policyPage.pageSize, type).then(
        res => {
          this.policyPage.total = res.data.total
          this.listNewsData = res.data.records
          console.log(res)
          const d = JSON.stringify(res.data.records)
          localStorage.setItem(name, d)
        }
      )
    },
    // 获取业内活动列表
    industryNews(type, name) {
      listNews(
        this.IndustryPage.pageIndex,
        this.IndustryPage.pageSize,
        type
      ).then(res => {
        this.IndustryPage.total = res.data.total
        this.listNewsData = res.data.records
        console.log(res)
        const d = JSON.stringify(res.data.records)
        localStorage.setItem(name, d)
      })
    },
    GoDetail(id) {
      if (this.active == 0) {
        this.$router.push({
          path: '/information-detail',
          query: {
            type: 3,
            id
          }
        })
      }
      if (this.active == 1) {
        this.$router.push({
          path: '/information-detail',
          query: {
            type: 2,
            id
          }
        })
      }
      if (this.active == 2) {
        this.$router.push({
          path: '/information-detail',
          query: {
            type: 1,
            id
          }
        })
      }
    },
    // 页个数数改变的时候可获取当前显示个数
    handleSizeChange(size) {
      this.pageSize = size
    },
    // 页数改变的时候可获取当前显示当前页码数
    handleCurrentChange(currentPage) {
      if (this.active == 0) {
        this.consultPage.pageIndex = currentPage
        this.listNews(3, 'listNewsData')
      }
      if (this.active == 1) {
        this.policyPage.pageIndex = currentPage
        this.policyNews(2, 'policyNewsData')
      }
      if (this.active == 2) {
        this.IndustryPage.pageIndex = currentPage
        this.industryNews(1, 'industryNewsData')
      }
    }
  }
}
</script>

<style lang="scss" scoped>
.search {
  background: #f5f5f5;
  .container {
    .corner {
      margin: 6px 0;
      font-size: 12px;
      color: #707070;
    }
    .tab {
      background: #fafafa;
      .tab-list {
        display: flex;
        justify-content: space-between;
        height: 45px;
        line-height: 45px;
        width: 306px;
        padding-left: 40px;
        li {
          cursor: pointer;
        }
        .pitch {
          border-bottom: 2px solid #ff3b30;
          color: #ff3b30;
        }
        .unchecked {
          border-bottom: none;
        }
      }
    }
    .list-box {
      background: #ffffff;
      margin-bottom: 60px;
      cursor: pointer;
      .detail-list {
        padding: 0px 50px;

        .box {
          display: flex;
          border-bottom: 1px solid #e5e5e5;
          padding: 30px 0;
          .left-contiainer {
            width: 228px;
            height: 132px;

            margin-right: 38px;
            .no-img {
              width: 100%;
              height: 100%;
              background: orchid;
            }
            img {
              width: 100%;
              height: 100%;
              // margin-right: 38px;
              object-fit: cover;
            }
          }

          .right {
            position: relative;
            .title {
              color: #333333;
              font-size: 16px;
              font-weight: bold;
              margin-bottom: 20px;
            }
            .text {
              width: 830px;
            }
            .time {
              position: absolute;
              bottom: 0;
              color: #c7c7cc;
            }
          }
        }
      }
    }
  }
  .el-pagination {
    height: 130px;
    display: flex;
    justify-content: center;
    align-items: center;
    // margin-top: 20px;
    background-color: #fff;
  }
  ::v-deep .el-pagination.is-background .el-pager li:not(.disabled).active {
    background-color: #fff;
    color: #ff3b30ff;
    border: 1px solid #ff3b30ff;
  }

  ::v-deep .el-pagination.is-background .el-pager li:not(.disabled) {
    // background-color: #fff;
    border: 1px solid #ddddddff;
  }
  ::v-deep .el-pagination.is-background .btn-prev {
    border: 1px solid #ddddddff;
    padding: 0 15px;
  }
  ::v-deep .el-icon-arrow-left:before {
    content: "上一页";
  }
  ::v-deep .el-pagination.is-background .btn-next {
    border: 1px solid #ddddddff;
    padding: 0 15px;
  }
  ::v-deep .el-icon-arrow-right:before {
    content: "下一页";
  }
}
</style>
